package com.ktgj.flight.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.flight.domain.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.flight.domain.FinanceConsignPymtRecord;
import com.ktgj.flight.service.IFinanceConsignPymtRecordService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 财务机票代收款记录Controller
 * 
 * @author ruoyi
 * @date 2022-06-13
 */
@RestController
@RequestMapping("/pymtRecord")
public class FinanceConsignPymtRecordController extends BaseController
{
    @Autowired
    private IFinanceConsignPymtRecordService financeConsignPymtRecordService;

    /**
     * 查询财务机票代收款记录列表
     */
    @PreAuthorize(hasPermi = "system:record:list")
    @GetMapping("/list")
    public TableDataInfo list(FinanceConsignPymtRecord financeConsignPymtRecord)
    {
        startPage();
        List<FinanceConsignPymtRecord> list = financeConsignPymtRecordService.selectFinanceConsignPymtRecordList(financeConsignPymtRecord);
        return getDataTable(list);
    }

    /**
     * 导出财务机票代收款记录列表
     */
    @PreAuthorize(hasPermi = "system:record:export")
    @Log(title = "财务机票代收款记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FinanceConsignPymtRecord financeConsignPymtRecord) throws IOException
    {
        List<FinanceConsignPymtRecord> list = financeConsignPymtRecordService.selectFinanceConsignPymtRecordList(financeConsignPymtRecord);
        ExcelUtil<FinanceConsignPymtRecord> util = new ExcelUtil<FinanceConsignPymtRecord>(FinanceConsignPymtRecord.class);
        util.exportExcel(response, list, "财务机票代收款记录数据");
    }

    /**
     * 获取财务机票代收款记录详细信息
     */
    @PreAuthorize(hasPermi = "system:record:query")
    @GetMapping(value = "/{consignPymtRecordId}")
    public AjaxResult getInfo(@PathVariable("consignPymtRecordId") Long consignPymtRecordId)
    {
        return AjaxResult.success(financeConsignPymtRecordService.selectFinanceConsignPymtRecordById(consignPymtRecordId));
    }

    /**
     * 新增财务机票代收款记录
     */
    @PreAuthorize(hasPermi = "system:record:add")
    @Log(title = "财务机票代收款记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FinanceConsignPymtRecord financeConsignPymtRecord)
    {
        return toAjax(financeConsignPymtRecordService.insertFinanceConsignPymtRecord(financeConsignPymtRecord));
    }

    /**
     * 修改财务机票代收款记录
     */
    @PreAuthorize(hasPermi = "system:record:edit")
    @Log(title = "财务机票代收款记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FinanceConsignPymtRecord financeConsignPymtRecord)
    {
        return toAjax(financeConsignPymtRecordService.updateFinanceConsignPymtRecord(financeConsignPymtRecord));
    }

    /**
     * 删除财务机票代收款记录
     */
    @PreAuthorize(hasPermi = "system:record:remove")
    @Log(title = "财务机票代收款记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{consignPymtRecordIds}")
    public AjaxResult remove(@PathVariable Long[] consignPymtRecordIds)
    {
        return toAjax(financeConsignPymtRecordService.deleteFinanceConsignPymtRecordByIds(consignPymtRecordIds));
    }

    /**
     * 代收记录列表
     * */
    @PostMapping("/flightOrderInfoList")
    public AjaxResult flightOrderInfoList(@RequestBody FinanceConsignPymtRecordIdVo financeConsignPymtRecordIdVo) {
        startPage();
        TmpFlightOrderInfoVo tmpFlightOrderInfoVo = financeConsignPymtRecordService.flightOrderInfoList(financeConsignPymtRecordIdVo);
        return AjaxResult.success(tmpFlightOrderInfoVo);
    }

    /**
     * 查询待修改的订单支付信息
     * */
    @PostMapping("/updatePayTypeVo")
    public AjaxResult updatePayTypeVo(@RequestBody Map<String, Object> map) {
        String orderNum = map.get("orderNum").toString();
        UpdatePayTypeVo updatePayTypeVo;
        try {
            updatePayTypeVo = financeConsignPymtRecordService.updatePayTypeVo(orderNum);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(updatePayTypeVo);
    }

    /**
     * 保存订单支付信息
     * */
    @PostMapping("/savePay")
    public AjaxResult savePay(HttpServletRequest request, @RequestBody UpdatePayTypeVo updatePayTypeVo){
        int i;
        try {
            i = financeConsignPymtRecordService.savePay(request, updatePayTypeVo);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (i > 0){
            return AjaxResult.success("保存成功");
        }
        else {
            return AjaxResult.success("保存失败");
        }
    }

    /**
     * 追加备注
     * */
    @PostMapping("/appendRemarks")
    public AjaxResult appendRemarks(@RequestBody Map<String, Object> map){
        Long orderId = Long.valueOf(map.get("orderNum").toString());
        String remark = null;
        if (map.get("remark") != null && !map.get("remark").equals("")){
            remark = map.get("remark").toString();
        }
        AppendRemarksVo appendRemarksVo;
        try {
            appendRemarksVo = financeConsignPymtRecordService.appendRemarks(orderId, remark);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(appendRemarksVo);
    }

    /**
     * 网络处理回显
     * */
    @PostMapping("/echo")
    public AjaxResult echo(@RequestBody Map<String, Object> map){
        Long orderId = Long.valueOf(map.get("orderNum").toString());
        UpdatePayTypeVo updatePayTypeVo;
        try {
            updatePayTypeVo = financeConsignPymtRecordService.echo(orderId);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        return AjaxResult.success(updatePayTypeVo);
    }

    /**
     * 网络处理
     * */
    @PostMapping("/networkProcessing")
    public AjaxResult networkProcessing(HttpServletRequest request, @RequestBody UpdatePayTypeVo updatePayTypeVo){
        String processing;
        try {
            processing = financeConsignPymtRecordService.networkProcessing(request, updatePayTypeVo);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(processing);
    }

    /**
     * 支付记录录入会员名称
     * */
    @PostMapping("/recordCustomerName")
    public AjaxResult recordCustomerName(@RequestBody Map<String, Object> map){
        Long orderId = Long.valueOf(map.get("orderNum").toString());
        RecordVo recordVo;
        try {
            recordVo = financeConsignPymtRecordService.recordCustomerName(orderId);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(recordVo);
    }
}
